package com.emenu.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.emenu.bean.BusHitDetail;
import com.emenu.bean.BusHitMaster;
import com.emenu.bean.BusSerieshit;
import com.emenu.bean.Localparm;
import com.emenu.bean.Menu;
import com.emenu.bean.PrintBusRetailDetail;
import com.emenu.bean.PubPrinter;
import com.emenu.bean.PubPrinterTable;
import com.emenu.bean.PubSeries;
import com.emenu.bean.PubSeriesGroup;
import com.emenu.bean.PubTable;
import com.emenu.bean.PubTimer;
import com.emenu.bean.SubMenu;
import com.emenu.bean.User;
import com.emenu.bean.normalItem;

import android.util.Log;

public class JsonUntil {
	/**
	 * json工具类
	 * {"msg":2,"success":true,"arrayData":[{"id":1,"name":"阿萨德"},{"id":2
	 * ,"name":"水电费"}]}
	 * 
	 * @param json格式字符
	 * @return List
	 * @throws Exception
	 */
	public static List<Map<String, String>> getJSONObject(String json)
			throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = null;

		JSONObject jsonObject = new JSONObject(json);
		String msg = jsonObject.getString("msg");
		Boolean success = jsonObject.getBoolean("success");

		JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject item = jsonArray.getJSONObject(i);
			int id = item.getInt("id");
			String name = item.getString("name");

			map = new HashMap<String, String>();
			map.put("id", id + "");
			map.put("name", name);
			list.add(map);
		}

		/**********
		 * 测试******************
		 * 
		 * for (Map<String, String> list2 : list) { String id = list2.get("id");
		 * String name = list2.get("name"); Log.i("abc", "id:" + id + " | name:"
		 * + name); }
		 ****/
		return list;
	}

	/**
	 * Sys_user select code,password,name,dinner_code,sex,industryNo,is_operate,
	 * is_posmanager,status from Sys_user where status=1
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static List<User> getUserList(String json) throws Exception {
		List<User> list = new ArrayList<User>();

		JSONObject jsonObject = new JSONObject(json);
		// String msg=jsonObject.getString("msg");
		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);

				User user = new User();
				user.setCode(item.getString("code"));
				user.setPassword(item.getString("password"));
				user.setName(item.getString("name"));
				user.setDinner_code(item.getString("dinner_code"));
				user.setIndustryNo(item.getString("industryNo"));
				if (item.getBoolean("sex")) {
					user.setSex(1);
				} else {
					user.setSex(0);
				}
				if (item.getBoolean("is_operate")) {
					user.setIs_operate(1);
				} else {
					user.setIs_operate(0);
				}
				if (item.getBoolean("is_posmanager")) {
					user.setIs_posmanager(1);
				} else {
					user.setIs_posmanager(0);
				}
				user.setStatus(item.getInt("status"));

				list.add(user);
			}

		}

		return list;
	}

	/**
	 * Pub_menu select
	 * code,name,picoldver,picnewver,picpath,visible,remark,status from pub_menu
	 * where status=1
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static List<Menu> getMenuList(String json) throws Exception {
		List<Menu> list = new ArrayList<Menu>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);

				Menu menu = new Menu();
				menu.setCode(item.getString("code"));
				menu.setName(item.getString("name"));
				menu.setPicoldver(item.getString("picoldver"));
				menu.setPicnewver(item.getString("picnewver"));
				menu.setPicpath(item.getString("picpath"));
				menu.setVisible(item.getBoolean("visible") ? 1 : 0);
				// if(item.getBoolean("visible")){
				// menu.setVisible(1);
				// }else{
				// menu.setVisible(0);
				// }
				menu.setRemark(item.getString("remark"));
				if (item.getBoolean("status")) {
					menu.setStatus(1);
				} else {
					menu.setStatus(0);
				}
				list.add(menu);
			}

		}

		return list;
	}

	/**
	 * Pub_submenu SELECT
	 * code,menu_code,name,PantryInteral,GarnishInteral,helpcode,tag_code
	 * ,menutype,rljtype,defaultamount,minamount,hitamount,dianpincode,price
	 * ,is_Special,SpecialPrice,rebate,is_alter,alter_integral,AdditionalPrint
	 * ,picoldver
	 * ,picnewver,picpath,upcode,is_Node,is_Serise,visible,remark,status
	 * Pub_submenu
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static List<SubMenu> getSubMenuList(String json) throws Exception {
		List<SubMenu> list = new ArrayList<SubMenu>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				SubMenu submenu = new SubMenu();
				submenu.setCode(item.getString("code"));
				submenu.setMenu_code(item.getString("menu_code"));
				submenu.setName(item.getString("name"));
				submenu.setPantryInteral(item.getInt("PantryInteral"));
				submenu.setGarnishInteral(item.getInt("GarnishInteral"));

				submenu.setHelpcode(item.getString("helpcode"));
				submenu.setTag_code(item.getString("tag_code"));
				submenu.setTag_name(item.getString("tag_name"));
				submenu.setMenutype(item.getString("menutype"));
				submenu.setRljtype(item.getInt("rljtype"));
				submenu.setDefaultamount(item.getInt("defaultamount"));

				submenu.setMinamount(item.getInt("minamount"));
				submenu.setHitamount(item.getInt("hitamount"));
				submenu.setDianpincode(item.getInt("dianpincode"));
				submenu.setPrice(item.getDouble("price"));
				submenu.setIs_Special(item.getBoolean("is_Special") ? 1 : 0);

				submenu.setSpecialPrice(item.getDouble("SpecialPrice"));
				submenu.setRebate(item.getBoolean("rebate") ? 1 : 0);
				submenu.setIs_alter(item.getBoolean("is_alter") ? 1 : 0);
				submenu.setAlter_integral(item.getInt("alter_integral"));
				submenu.setAdditionalPrint(item.getInt("AdditionalPrint"));

				submenu.setPicoldver(item.getString("picoldver"));
				submenu.setPicnewver(item.getString("picnewver"));
				submenu.setPicpath(item.getString("picpath"));
				submenu.setUpcode(item.getString("upcode"));

				submenu.setIs_Node(item.getBoolean("is_Node") ? 1 : 0);
				submenu.setIs_Serise(item.getBoolean("is_Serise") ? 1 : 0);
				submenu.setVisible(item.getBoolean("visible") ? 1 : 0);
				submenu.setRemark(item.getString("remark"));
				submenu.setStatus(item.getBoolean("status") ? 1 : 0);

				list.add(submenu);
			}

		}

		return list;
	}

	/**
	 * Pub_submenu SELECT
	 * code,menu_code,name,PantryInteral,GarnishInteral,helpcode,tag_code
	 * ,menutype,rljtype,defaultamount,minamount,hitamount,dianpincode,price
	 * ,is_Special,SpecialPrice,rebate,is_alter,alter_integral,AdditionalPrint
	 * ,picoldver
	 * ,picnewver,picpath,upcode,is_Node,is_Serise,visible,remark,status
	 * Pub_submenu
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static List<Localparm> getLocalParmList(String json)
			throws Exception {
		List<Localparm> list = new ArrayList<Localparm>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				Localparm LL = new Localparm();
				LL.setCode(item.getString("code"));
				LL.setName(item.getString("name"));
				LL.setParmvalue(item.getString("parmvalue"));
				list.add(LL);
			}

		}

		return list;
	}

	/**
	 * PubTable
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 *             selecta.id,a.code,a.name,a.helpcode,a.dinner_code,a.
	 *             table_type, b.name table_typename, a.MaxCount,
	 *             a.MinCount,a.price from Pub_table a,pub_tabletype b where
	 *             a.table_type=b.code and a.status=1;
	 */

	public static List<PubTable> getPubTableList(String json) throws Exception {
		List<PubTable> list = new ArrayList<PubTable>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubTable LL = new PubTable();
				LL.setCode(item.getString("code"));
				LL.setName(item.getString("name"));
				LL.setHelpcode(item.getString("helpcode"));
				LL.setDinner_code(item.getString("dinner_code"));
				LL.setTable_type(item.getString("table_type"));
				LL.setTable_typename(item.getString("table_typename"));

				LL.setMaxCount(item.getInt("MaxCount"));
				LL.setMinCount(item.getInt("MinCount"));
				LL.setPrice(item.getDouble("price"));

				list.add(LL);
			}

		}

		return list;
	}

	/***
	 * PubTimer
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 *             select code,name from pub_timer
	 */
	public static List<PubTimer> getPubTimerList(String json) throws Exception {
		List<PubTimer> list = new ArrayList<PubTimer>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubTimer LL = new PubTimer();
				LL.setCode(item.getString("code"));
				LL.setName(item.getString("name"));
				LL.setC1(item.getString("c1"));
				LL.setC3(item.getString("c2"));
				LL.setC3(item.getString("c3"));
				list.add(LL);
			}

		}

		return list;
	}

	/***
	 * PUB_SERIES
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 *             select a.id,a.series_code,a.seriesgroupcode,a.submenu_code,
	 *             a.amount,b.name submenu_name from pub_series a ,pub_submenu b
	 *             where a.submenu_code=b.code
	 */
	public static List<PubSeries> getPubSeriesList(String json)
			throws Exception {
		List<PubSeries> list = new ArrayList<PubSeries>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubSeries LL = new PubSeries();
				LL.setId(item.getInt("id"));
				LL.setSeries_code(item.getString("series_code"));
				LL.setSeriesgroupcode(item.getString("seriesgroupcode"));
				LL.setSubmenu_code(item.getString("submenu_code"));
				LL.setSubmenu_name(item.getString("submenu_name"));
				LL.setAmount(item.getDouble("amount"));

				list.add(LL);
			}

		}

		return list;
	}

	public static List<PubSeriesGroup> getPubSeriesGroupList(String json)
			throws Exception {
		List<PubSeriesGroup> list = new ArrayList<PubSeriesGroup>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubSeriesGroup LL = new PubSeriesGroup();
				LL.setId(item.getInt("id"));
				LL.setCode(item.getString("code"));
				LL.setName(item.getString("name"));

				LL.setIsselect(item.getBoolean("isselect") ? 1 : 0);
				LL.setSelectcount(item.getDouble("selectcount"));

				list.add(LL);
			}

		}

		return list;
	}
	//从service得到BusHitMaster
	public static List<BusHitMaster> getBusHitMasterList(String json)
			throws Exception {
		List<BusHitMaster> list = new ArrayList<BusHitMaster>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				BusHitMaster LL = new BusHitMaster();
				 
				LL.setCode(item.getString("code"));
				LL.setHitcode("hitcode");
				LL.setHit_typeName(item.getString("hit_typename"));
				LL.setAmount(item.getInt("amount"));
				LL.setCreatedatetime(item.getString("createdatetime"));
				LL.setCreateuserName(item.getString("name"));
				
				list.add(LL);
			}

		}

		return list;
	}
	
	
	//从service得到BusHitDetail
	public static List<BusHitDetail> getBusHitDetailList(String json)
			throws Exception {
		List<BusHitDetail> list = new ArrayList<BusHitDetail>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				BusHitDetail LL = new BusHitDetail();
				 
				LL.setMenucode(item.getString("menu_code"));
				LL.setMenuname(item.getString("menu_name"));
				LL.setPrice(item.getDouble("price"));
				LL.setMenucode(item.getString("menu_code"));
				LL.setMenucode(item.getString("menu_code"));
				LL.setAmount(item.getInt("amount"));
				LL.setAmount_sum(item.getDouble("amount_sum"));
				LL.setMenu_type(item.getString("menutype"));
				LL.setRljtype(item.getInt("rljtype"));
				LL.setRljtypeName(item.getString("rljtypename"));
								
				list.add(LL);
			}

		}

		return list;
	}
	
	//从service得到PubPrinter
	public static List<PubPrinter> getPubPrinterList(String json)
			throws Exception {
		List<PubPrinter> list = new ArrayList<PubPrinter>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubPrinter LL = new PubPrinter();
				 
				LL.setId(item.getInt("id"));
				LL.setCode(item.getString("code"));
				LL.setPrintdesc(item.getString("printdesc"));
				LL.setPrintaddress(item.getString("printaddress"));				 
								
				list.add(LL);
			}

		}

		return list;
	}

	//从service得到Pub_printertable
	public static List<PubPrinterTable> getPubPrinterTableList(String json)
			throws Exception {
		List<PubPrinterTable> list = new ArrayList<PubPrinterTable>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PubPrinterTable LL = new PubPrinterTable();
				 
				LL.setId(item.getInt("id"));
				LL.setPrint_code(item.getString("print_code"));
				LL.setTable_code(item.getString("table_code"));
				LL.setPrintdesc(item.getString("printdesc"));
				LL.setPrintaddress(item.getString("printaddress"));

				list.add(LL);
			}

		}

		return list;
	}
	
	//从service得到PrintBusRetailDetail
	public static List<PrintBusRetailDetail> getPrintBusRetailDetailList(String json)
			throws Exception {
		List<PrintBusRetailDetail> list = new ArrayList<PrintBusRetailDetail>();

		JSONObject jsonObject = new JSONObject(json);

		Boolean success = jsonObject.getBoolean("success");
		if (success) {

			JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject item = jsonArray.getJSONObject(i);

				PrintBusRetailDetail LL = new PrintBusRetailDetail();
				 
				LL.setRetail_code(item.getString("单据编号"));
				LL.setMenu_code(item.getString("菜品编号"));
				LL.setMenu_name(item.getString("菜品名称"));
				LL.setTagname(item.getString("单位"));
				LL.setHit_typeName(item.getString("点菜类型"));
				LL.setAmount(item.getDouble("下单数量"));
				LL.setOperatetime(item.getString("下单时间"));
				LL.setComeamount(item.getDouble("已配菜"));
				LL.setCometime(item.getString("配菜时间"));
				LL.setGoamount(item.getDouble("已传菜"));
				LL.setGotime(item.getString("传菜时间"));
				LL.setReminderfoodName(item.getString("催菜"));
				LL.setCalledSingleName(item.getString("叫单"));
				list.add(LL);
			}

		}

		return list;
	}

	// 封装成json字符串把BusHitDetail发送给服务器
	public static String BHDListToJson(List<BusHitDetail> list) {
		JSONArray array = new JSONArray();
		JSONObject objmain = new JSONObject();
		try {
			for (BusHitDetail bhd : list) {
				JSONObject obj = new JSONObject();
				obj.put("ID", bhd.getID());
				obj.put("hit_code", bhd.getHit_code());
				obj.put("menu_code", bhd.getMenucode());
				obj.put("menu_name", bhd.getMenuname());
				obj.put("price", bhd.getPrice());
				obj.put("amount", bhd.getAmount());
				obj.put("tag_code", bhd.getTag_code());
				obj.put("amount_sum", bhd.getAmount_sum());
				obj.put("menutype", bhd.getMenu_type());
				
				obj.put("rljtype", bhd.getRljtype());
				obj.put("CalledSingle", bhd.getCalledSingle());
				obj.put("remark ", bhd.getRemark());
				obj.put("upload ", bhd.getUpload());
				obj.put("C1 ", bhd.getC1());
				obj.put("C2 ", bhd.getC2());
				obj.put("C3 ", bhd.getC3());
				obj.put("C4 ", bhd.getC4());

				array.put(obj);

			}
			objmain.put("arrayData", array);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objmain.toString();
	}

	// 封装成json字符串把busHitMaster发送给服务器
	public static String BHMListToJson(List<BusHitMaster> list) {
		JSONArray array = new JSONArray();
		JSONObject objmain = new JSONObject();
		try {
			for (BusHitMaster bhm : list) {
				JSONObject obj = new JSONObject();
				obj.put("ID", bhm.getId());
				obj.put("code", bhm.getCode());
				obj.put("hitcode", bhm.getHitcode());
				obj.put("dinner_code", bhm.getDinner_code());
				obj.put("table_code", bhm.getTable_code());
				obj.put("consume_date", bhm.getConsume_date());
				
				obj.put("time_code", bhm.getTime_code());
				obj.put("Manager_code", bhm.getManager_code());
				obj.put("amount", bhm.getAmount());
				obj.put("hit_type", bhm.getHit_type());
				obj.put("printcount", bhm.getPrintcount());
				
				obj.put("printuser", bhm.getPrintuser());
				obj.put("printdatetime", bhm.getPrintdatetime());
				
				obj.put("createuser", bhm.getCreateuser());
				obj.put("createdatetime", bhm.getCreatedatetime());
				obj.put("updateuser ", bhm.getUpdateuser());
				obj.put("updatedatetime", bhm.getUpdatedatetime());
				obj.put("remark", bhm.getRemark());
				obj.put("upload", bhm.getUpload());
				obj.put("C1", bhm.getC1());
				obj.put("C2", bhm.getC2());
				obj.put("C3", bhm.getC3());
				obj.put("C4", bhm.getC4());
				
				array.put(obj);

			}
			objmain.put("arrayData", array);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objmain.toString();
	}
	
	// 封装成json字符串把Bus_Series_hit发送给服务器
	public static String BusSerieshitListToJson(List<BusSerieshit> list) {
		JSONArray array = new JSONArray();
		JSONObject objmain = new JSONObject();
		try {
			for (BusSerieshit bsh : list) {
				JSONObject obj = new JSONObject();
				obj.put("id", bsh.getId());
				obj.put("hit_code", bsh.getHit_code());
				obj.put("series_code", bsh.getSeries_code());
				obj.put("seriesgroupcode", bsh.getSeriesgroupcode());
				obj.put("submenu_code", bsh.getSubmenu_code());
				obj.put("amount", bsh.getAmount());
				obj.put("upload", bsh.getUpload());
				obj.put("c1", bsh.getC1());
				obj.put("c2", bsh.getC2());
				obj.put("c3", bsh.getC3());
				 

				array.put(obj);

			}
			objmain.put("arrayData", array);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objmain.toString();
	}

	/** */
	/**
	 * 利用MD5进行加密
	 * 
	 * @param str
	 *            待加密的字符串
	 * @return 加密后的字符串
	 * @throws NoSuchAlgorithmException
	 *             没有这种产生消息摘要的算法
	 * @throws UnsupportedEncodingException
	 * 
	 *             获取32位md5加密密文:getMd5Value("sub");
	 *             获取16位md5加密密文:getMd5Value("sub").substring(8, 24);
	 */
	public static String getMd5Value(String sSecret) {
		try {
			MessageDigest bmd5 = MessageDigest.getInstance("MD5");
			bmd5.update(sSecret.getBytes());
			int i;
			StringBuffer buf = new StringBuffer();
			byte[] b = bmd5.digest();
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 
	 * @param pw
	 *            input
	 * @param pwmd5
	 *            md5
	 * @return
	 */
	public static boolean isPassword(String pw, String pwmd5) {
		if (getMd5Value(pw).equals(pwmd5)) {

			return true;
		}
		return false;

	}

}
